<?php

namespace App\Exports\Scan;

use Curl\CurlService\CurlService;
use Maatwebsite\Excel\Concerns\FromCollection;

class FlightExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        //

        $param = [];
        $request = request();
        if ($request->task_id) {

            $param['task_id'] = $request->task_id;
        }
        if ($request->airline) {
            $param['hs_code'] = $request->airline;
        }
        if ($request->depart_code) {
            $param['depart_city_code'] = $request->depart_city_code;
        }

        if ($request->arrived_code) {
            $param['arrived_city_code'] = $request->arrived_city_code;
        }

        $data[] = [
            '任务ID',
            '航班',
            '起飞',
            '到达',
            '日期',
            '舱位',
            '价格',
            '座位数',
            '代理费'
        ];

        $res = CurlService::get(env('CRM_URL').'/flight/allLists?'.http_build_query($param));
        if ($res && isset($res['errcode']) && $res['errcode'] == 0 && !empty($res['data'])) {

            foreach ($res['data'] as $key=>$value) {
                $data[] = [
                    $value['task_id'],
                    $value['flight_line']['hs_code'].$value['flight_code'],
                    $value['flight_line']['depart_city_code'],
                    $value['flight_line']['arrived_city_code'],
                    $value['depart_at'],
                    $value['cabins'][0]['cabin_code'],
                    $value['cabins'][0]['price'],
                    $value['cabins'][0]['seat'],
                    $value['cabins'][0]['rate'] ? : '0',
                ];
            }
        }

        return collect($data);

    }
}
